package org.example.Day4;


import java.util.Arrays;
import java.util.Comparator;
import java.util.function.IntToDoubleFunction;

public class ArraysTest1 {
    public static void main(String[] args){
        //toString (类型[])返回数组内容
        int [] arr = {10,20,30,40,50};
        System.out.println(Arrays.toString(arr));

        //类型[] copyOfRange(类型[] arr,初始索引，结束索引);拷贝数组(指定范围 包前不包后)
        int [] arr2 = Arrays.copyOfRange(arr, 1,4);
        System.out.println(Arrays.toString(arr2));

        //copyOf 拷贝数组(类型[] arr, int newlength 可以指定长度)
        int [] arr3 = Arrays.    copyOf(arr, 10);
        System.out.println(Arrays.toString(arr3));

        //setAll(double [] array,IntToDoubleFunctiongenerator)把数组中的数据改为新数据输入进去
        double [] prices = {99.8,129,100};
        //打八折存进去
//        Arrays.setAll(prices, new IntToDoubleFunction() {
//            @Override
//            public double applyAsDouble(int value) {
//                return prices[value]*0.8;
//            }
//        });

        /*
        改成lamdba表达式改写 简单
         */
        Arrays.setAll(prices,(int value)->{
            return prices[value]*0.8;
        });
        System.out.println(Arrays.toString(prices));
        //sort(类型[] arr)对数组进行排列
        Arrays.sort(prices);
        System.out.println(Arrays.toString(prices));
        System.out.println("-----------------------------------------------");

        Student[] students = new Student[4];
        students[0] = new Student("蜘蛛精", 169.5, 23);
        students[1] = new Student("紫霞", 163.8, 26);
        students[2] = new Student("紫霞", 163.8, 26);
        students[3] = new Student("⾄尊宝", 167.5, 24);
        //3.对数组中的元素按照重量升序排列: 先⽤匿名内部类写法
//        Arrays.sort(students, new Comparator<Student>() {
//                @Override
//                public int compare(Student o1, Student o2) {
//                        return Double.compare(o1.getHeight(), o2.getHeight()); // 升序
//                }
//        });
        //改用lambda写法
        Arrays.sort(students,(Student o1,Student o2)->{
            return Double.compare(o1.getHeight(),o2.getHeight());
        });
        System.out.println(Arrays.toString(students));
    }
}
